<?php

/**
 * ContactPerson form.
 *
 * @package    form
 * @subpackage ContactPerson
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class ContactPersonForm extends BasePersonForm
{
  public function configure()
  {
	
		unset(
      $this['created_at'], $this['updated_at'],$this['author'], $this['visible']
    );

    $this->widgetSchema['type'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['client_id'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['person_id'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['author'] = new sfWidgetFormInputHidden();

		 if (!$this->isNew()) {  
  // embed all contact forms
  foreach ($this->getObject()->getContact() as $contact) {

  // create a new subcategory form for the current subcategory model object
  $contactForm = new ContactForm($contact);

  // embed the subcategory form in the main category form
  $this->embedForm('contact', $contactForm);

	   }	
		}else{
	// create a new subcategory form for a new subcategory model object
  $contactForm = new ContactForm();

  // embed the subcategory form in the main category form
  $this->embedForm('contact', $contactForm);

	}
	
	
	
		$phonesForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPhone() as $phone) {
	   	$phonesForm->embedForm('phone'.$phone->getId(), new PhoneForm($phone));
	   }
		}
		//create blank phone form at end
		 $phonesForm->embedForm('phone', new PhoneForm());
		 $this->embedForm('phones', $phonesForm);



		$websitesForm = new sfForm();
		 if (!$this->isNew()) {  
	    // embed all Website forms
	    foreach ($this->getObject()->getWebsites() as $website) {
	    $websitesForm->embedForm('website'.$website->getId(), new WebsitesForm($website));
		   }
		}
	//create blank website form
	  $websitesForm->embedForm('website', new WebsitesForm());
	  $this->embedForm('websites', $websitesForm);



			$emailsForm = new sfForm();
			if (!$this->isNew()) {  
		   // embed all old email forms
		   foreach ($this->getObject()->getEmail() as $email) {
		   	$emailsForm->embedForm('email'.$email->getId(), new EmailForm($email));
		   }
			}
			//create blank email form at end
			 $emailsForm->embedForm('email', new EmailForm());
			 $this->embedForm('emails', $emailsForm);



				$smasForm = new sfForm();
				if (!$this->isNew()) {  
			   // embed all old SMA forms
			   foreach ($this->getObject()->getSocialMediaAccount() as $sma) {
			   	$smasForm->embedForm('socialmediaaccount'.$sma->getId(), new SocialMediaAccountForm($sma));
			   }
				}
				//create blank SMA form at end
				 $smasForm->embedForm('socialmediaaccount', new SocialMediaAccountForm());
				 $this->embedForm('socialmediaaccounts', $smasForm);

	

	$this->validatorSchema->setOption('allow_extra_fields', true);
	
  }


  /**
   * Update the client_id of all the forms after the booking has been saved
   */
  public function saveEmbeddedForms($con = null, $forms = null)
  {    
	/*
    foreach($this->getEmbeddedForms() as $allForms)
    {
      if (!$allForms->getObject()->getPersonId())
      {
        $allForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }
*/
foreach($this->embeddedForms['phones']->getEmbeddedForms() as $phonesForms)
{
  if (!$phonesForms->getObject()->getPersonId())
  {
    $phonesForms->getObject()->setPersonId($this->getObject()->getId());
		//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
  }
}

foreach($this->embeddedForms['websites']->getEmbeddedForms() as $websitesForms)
{
  if (!$websitesForms->getObject()->getPersonId())
  {
    $websitesForms->getObject()->setPersonId($this->getObject()->getId());
		//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
  }
}

foreach($this->embeddedForms['emails']->getEmbeddedForms() as $emailsForms)
{
  if (!$emailsForms->getObject()->getPersonId())
  {
    $emailsForms->getObject()->setPersonId($this->getObject()->getId());
		//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
  }
}

foreach($this->embeddedForms['socialmediaaccounts']->getEmbeddedForms() as $smaForms)
{
  if (!$smaForms->getObject()->getPersonId())
  {
    $smaForms->getObject()->setPersonId($this->getObject()->getId());
		//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
  }
}



 if (!$this->embeddedForms['contact']->getObject()->getPersonId()){
	$contact = $this->embeddedForms['contact']->getObject();//set Contact
	$contact->setPersonId($this->getObject()->getId());
}

    parent::saveEmbeddedForms($con, $forms);
  }



	public function bind(array $taintedValues = null, array $taintedFiles = null)
	{
	  if (is_null($taintedValues['websites']['website']['url'])|| strlen($taintedValues['websites']['website']['url']) === 0 )
	  {
	    unset($this->embeddedForms['websites']['website']);
	    $this->validatorSchema['websites']['website'] = new sfValidatorPass();
	    $taintedValues['websites']['website'] = null;
	  }
	
		if (is_null($taintedValues['emails']['email']['email'])|| strlen($taintedValues['emails']['email']['email']) === 0 )
	  {
	    unset($this->embeddedForms['emails']['email']);
	    $this->validatorSchema['emails']['email'] = new sfValidatorPass();
	    $taintedValues['emails']['email'] = null;
	  }
	
	  if (is_null($taintedValues['socialmediaaccounts']['socialmediaaccount']['account_name'])|| strlen($taintedValues['socialmediaaccounts']['socialmediaaccount']['account_name']) === 0 )
	  {
	    unset($this->embeddedForms['socialmediaaccounts']['socialmediaaccount']);
	    $this->validatorSchema['socialmediaaccounts']['socialmediaaccount'] = new sfValidatorPass();
	    $taintedValues['socialmediaaccounts']['socialmediaaccount'] = null;
	  }
	
		if (is_null($taintedValues['phones']['phone']['number'])|| strlen($taintedValues['phones']['phone']['number']) === 0 )
	  {
	    unset($this->embeddedForms['phones']['phone']);
	    $this->validatorSchema['phones']['phone'] = new sfValidatorPass();
	    $taintedValues['phones']['phone'] = null;
	  }
	
	
	  parent::bind($taintedValues, $taintedFiles);
	}
	


}